package com.hellogood.yaohuo.server.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Test {
	public static void main(String[] args) {

		new Test().crossHost();
	}

	private void crossHost() {

		Connection conn = ConnectionPool.getInstance().getConnection();
		List<?> list = new ArrayList<Object>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String pluCode = "6927741715892";
		String orgCode = "C001";
		try {
			System.out.println("***********************");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S");
			sdf.format(new Date(System.currentTimeMillis()));
			System.out.println(sdf.format(new Date(System.currentTimeMillis())));
			String sql = "if not exists(select a.plucode,a.barcode,a.pluname,a.depcode, a.clscode,a.unit from goods a " + " where (a.plucode='"
					+ pluCode + "' or a.barcode='" + pluCode + "'))"
					+ " select a.plucode,a.barcode,a.pluname as pluName,a.depcode, a.clscode,a.unit,g.ojprice,g.price" + " from goods a,goodsorg g"
					+ " where a.plucode=(select b.plucode from barcoderef b where b.barcode='" + pluCode
					+ "')  and a.plucode=g.plucode and g.orgCode ='" + orgCode + "'";
			System.out.println(sql);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getString("pluName"));
			}
			System.out.println(sdf.format(new Date(System.currentTimeMillis())));
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				ConnectionPool.getInstance().release(conn);
			}
		}

	}

	private void crossDatabase() {
		Connection conn = ConnectionPool.getInstance().getConnection();
		List<?> list = new ArrayList<Object>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = "select top 5 pluName from sysdb..goods";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getString("pluName"));
			}
			System.out.println("**********************");
			sql = "select * from PsForm";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getString("PsbillNo"));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				ConnectionPool.getInstance().release(conn);
			}
		}

	}
}
